package com.lian.pet.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lian.pet.common.basic.enums.DataTypeEnum;
import com.lian.pet.common.cache.redis.service.RedisService;
import com.lian.pet.domain.dto.AddPetEventDTO;
import com.lian.pet.domain.dto.QueryPetEventDTO;
import com.lian.pet.domain.entity.PetEvent;
import com.lian.pet.domain.vo.PetEventVO;
import com.lian.pet.factory.ResponseBeanFactory;
import com.lian.pet.mapper.PetEventMapper;
import com.lian.pet.service.PetEventService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PetEventServiceImpl implements PetEventService {
        private final PetEventMapper petEventMapper;
        private final RedisService redisService;

        @Override
        public void addPetEvent(AddPetEventDTO req, Integer petId){
            PetEvent petEvent = ResponseBeanFactory.getPetEvent(req, petId);
            petEventMapper.insert(petEvent);
            // 添加数据时 删除Cache数据
            redisService.delKey(DataTypeEnum.PETEVENT.name());
            log.info("执行成功[新增宠物饲养记录]");
        }

        @Override
        public List<PetEventVO> queryPetEvent(QueryPetEventDTO req, Integer petId){
            IPage<PetEvent> page = new Page<>(req.getPageNum(),req.getPageSize());
            IPage<PetEvent> iPage = petEventMapper.selectPage(page, Wrappers.<PetEvent>lambdaQuery()
                    .eq(PetEvent::getPetId, petId)
                    .eq(req.getPetId()!=null, PetEvent::getPetId, req.getPetId())
                    .orderByDesc(PetEvent::getDate_start));
            List<PetEvent> petEvent = iPage.getRecords();
            if(petEvent.isEmpty()){
                return Collections.emptyList();
            }
            List<PetEventVO> petEventVOS = new ArrayList<>();
            petEvent.forEach(petevent -> {
                PetEventVO petEventVO = PetEventVO.fromPetEvent(petevent);
                petEventVOS.add(petEventVO);
            });

            log.info("执行成功[查询宠物活动列表]");
            return petEventVOS;
        }

        @Override
        public PetEventVO getPetEvent(Integer id){
            PetEvent petEvent = petEventMapper.selectById(id);
            return PetEventVO.fromPetEvent(petEvent);
        }

        @Override
        public void deletePetEvent(Integer id){
            petEventMapper.deleteById(id);
        }

        @Override
        public void updatePetEvent(Integer id, AddPetEventDTO req) {
            PetEvent existingPetEvent = petEventMapper.selectById(id);
            if (existingPetEvent == null) {
                // 如果找不到对应的宠物饲养信息，则抛出异常或者返回错误信息
                // 这里选择抛出一个运行时异常示例
                throw new RuntimeException("宠物活动信息不存在");
            }

            existingPetEvent.setName(req.getName());
            existingPetEvent.setType(req.getType());
            existingPetEvent.setLocation(req.getLocation());
            existingPetEvent.setDescription(req.getDescription());
            existingPetEvent.setDate_start(req.getDate_start());
            existingPetEvent.setDate_end(req.getDate_end());
            existingPetEvent.setCreateTime(new Date());

            petEventMapper.updateById(existingPetEvent);

            // 更新数据时 删除Cache数据
            redisService.delKey(DataTypeEnum.PETEVENT.name());

            log.info("执行成功[修改宠物活动信息]");
        }

}
